<?php


namespace app\admin\controller;


use app\BaseController;
use app\Request;

class Branch extends  BaseController
{
    public function getList(Request  $request)
    {
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $result = \app\admin\model\Branch::order('sort desc id desc')->page($page, $limit)->select();
        $total = \app\admin\model\Branch::count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $limit,
            'data' =>  $result
        ];

        return show(config('status.success'), '操作成功', $data);
    }

    /**
     * 创建
     * @param Request $request
     * @return \think\response\Json
     */
    public function create(Request $request)
    {
        $name = $request->post('name');
        \app\admin\model\Branch::create([
            'name' => $name,
            'sort' => $request->post('sort')
        ]);
        return show(config('status.success'), '操作成功');
    }

    /**
     * 更新
     * @param Request $request
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $name = $request->post('name');
        \app\admin\model\Branch::where(['id' => $request->post('id')])->update([
            'name' => $name,
            'sort' => $request->post('sort')
        ]);

        return show(config('status.success'), '操作成功');
    }

    public function delete(Request  $request)
    {
        \app\admin\model\Branch::destroy($request->post('id'));
        return show(config('status.success'), '操作成功');
    }
}